//
//  BottomOpeView.swift
//  ANOA
//
//  Created by 邱仙凯 on 2025/1/24.
//  Copyright © 2025 zwb. All rights reserved.
//  审批的多个按钮显示方式
let moreOpHH:Int = 38
class AuditOpeView:UIView {
    var mainarr = [UIButton]()// 主要的两个按钮
   var remainarr = [AuditOperButton]()//剩余的附属按钮
    var moreBtn:UIButton = {
        var v = UIButton()
        v.setTitle("更多", for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        v.setTitleColor(UIColor(kshex: "#8991B0"), for: .normal)
        return v
    }()
   
     var moreOpeV = MoreOpeAlertV()
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        layer.masksToBounds = true
        layer.cornerRadius = 5
        
        
  
        
    }
    // 按钮名称
    var operateBtnBlock:((String)->())?
    func setBtnArr(appr:AuditProcessAppear) {
         
        var aTitles:[String] = []
        if appr.jueseType == .auditer {
            // 同意  指定改单      回退  移交  加签  知会
            if PlatformManager.shared.platform == .zhuYouAn {
                aTitles = ["同意", refuseTitle, backTitle, "移交", "加签", "知会"]
            } else {
                aTitles = ["同意", refuseTitle, "指定改单", backTitle, "移交", "加签", "知会"]
            }
            
        }
        if appr.jueseType == .noticer {
            
            aTitles = ["评论","已阅"]
            // 评论   已阅
          }
        if appr.jueseType == .commenter {
            
            aTitles = ["评论"]
            // 评论   已阅
          }
        if appr.jueseType == .submiter {
            aTitles = ["取消提交","催办"]
            // 取消提交   催办
            
            
        }
        if appr.jueseType == .completed {
            aTitles = ["知会","已阅"]
            // 知会   已阅
            
        }
        if appr.jueseType == .bohui {
            //   已阅
            aTitles = ["已阅"]
            
        }
        if appr.jueseType == .writer {
            aTitles = ["保存单据","填写完成"]
          
        }
       
        for i in 0..<aTitles.count {
            let onx = aTitles[i]
            if i < 2 {
                let btn = UIButton()
                btn.layer.cornerRadius = 8
                btn.setTitle(onx, for: .normal)
                self.addSubview(btn)
                mainarr.append(btn)
            } else {
                let btn = AuditOperButton()
                btn.setTitle(onx, for: .normal)
                remainarr.append(btn)
            }
           
               
        }
        
        for itembtn in mainarr {
            itembtn.addTarget(self, action: #selector(audit(sender:)), for: .touchUpInside)
        }
        for itembtn in remainarr {
            itembtn.addTarget(self, action: #selector(audit(sender:)), for: .touchUpInside)
        }
        
        
        makeCons()
          
        
  

    }
    // 审批
    @objc func audit(sender: UIButton) {
        self.moreOpeV.dismiss()
        let btntitle = sender.titleLabel!.text.unwrap
        if let clo = self.operateBtnBlock {
            clo(btntitle)
        }
   
    }
    
   
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func makeCons() {
        // 审核的按钮布局
        let padding = 8
        let ww = (Int(KSGlobal.screenW - 10) - padding * 4)/3
        let hh = 40
 
       
        if mainarr.count == 1 {
            layoutBtn1()
        } else if mainarr.count == 2 {
            layoutBtn1()
            layoutBtn2()
        }
        
        if remainarr.count > 0 {
        
            addSubview(moreBtn)
            moreBtn.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(5)
                make.top.equalToSuperview().offset(10)
                make.width.equalTo(60)
                make.height.equalTo(hh)
            }
            self.moreBtn.layoutIfNeeded()
            let alertH:CGFloat = CGFloat(remainarr.count * moreOpHH)
            moreBtn.addTouchUpInSideAction { _ in
                for subV in self.moreOpeV.subviews {
                    subV.removeFromSuperview()
                }
                for i in 0..<self.remainarr.count {
                    let theBtn = self.remainarr[i]
               
                    self.moreOpeV.addSubview(theBtn)
                    theBtn.snp.makeConstraints { make in
                        make.left.right.equalToSuperview()
                        make.height.equalTo(moreOpHH)
                        make.top.equalToSuperview().offset(i * moreOpHH)
                    }
                }
        
                let ttframe = self.moreBtn.convert(self.moreBtn.bounds , to: self.moreBtn.window)
               
                self.moreOpeV.constrains { make in
                    make.width.equalTo(120)
                    make.height.equalTo(alertH)
                    make.top.equalToSuperview().offset(ttframe.minY - alertH - 10)
                    make.left.equalTo(ttframe.minX)
                }
                self.moreOpeV.present()
            }
            
        }
        
        func layoutBtn1() {
            let v = mainarr[0]
            v.backgroundColor = UIColor(kshex: "#4972F9")
            v.setTitleColor(.white, for: .normal)
            v.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
             v.snp.makeConstraints { make in
                 make.width.equalTo(125)
                 make.height.equalTo(hh)
                 make.right.equalToSuperview().offset(-10)
                 make.top.equalToSuperview().offset(10)
            }

        }
        func layoutBtn2() {
            let v = mainarr[1]
            v.backgroundColor = .white
            v.setTitleColor(UIColor(kshex: "#4972F9"), for: .normal)
            v.layer.borderWidth = 2
            v.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
            v.layer.borderColor = UIColor(kshex: "#4972F9").cgColor
            v.snp.makeConstraints { make in
                make.width.equalTo(125)
                make.height.equalTo(hh)
                make.right.equalTo(mainarr[0].snp.left).offset(-10)
                make.top.equalToSuperview().offset(10)
           }

        }
    }
   
}

class MoreOpeAlertV:UIView {
    var img:UIImageView = {
        var v = UIImageView()
        v.image = UIImage(named: "xiaosanjiao")
        return v
    }()
    private var _alertCover = UIView()
    
    private var _constrainsClosure: ((ConstraintMaker) -> Void)?
 
    func constrains(_ closure: @escaping (ConstraintMaker) -> Void) {
        _constrainsClosure = closure
    }
   
 
   func present() {
       let cover = UIView()
       cover.frame = UIScreen.main.bounds
        cover.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.1)
        cover.addSubview(self)
       let tap2 = UITapGestureRecognizer()
       tap2.numberOfTapsRequired = 1
       tap2.addTap { [unowned self] _ in
            
           self.dismiss()
        }
       
       cover.addGestureRecognizer(tap2)
      
       if let constraintsClosure = _constrainsClosure {
           self.snp.remakeConstraints(constraintsClosure)
       } else {
           self.snp.makeConstraints { make in
               make.center.equalToSuperview()
               make.width.equalTo(KSGlobal.screenW * 0.35)
               make.height.equalTo(KSGlobal.screenH * 0.5)
               
           }
       }
       
       cover.addSubview(img)
       img.snp.makeConstraints { make in
           make.width.equalTo(30)
           make.height.equalTo(8)
           make.top.equalTo(self.snp.bottom)
           make.left.equalTo(self).offset(15)
       }
       
       cover.alpha = 0
      
       if let kwindow =  UIApplication.shared.windows.filter({$0.isKeyWindow}).first {
           kwindow.rootViewController?.view.addSubview(cover)
           cover.layoutIfNeeded()
           self.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
           
           UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut) {
               cover.alpha = 1
               self.alpha = 1
               self.transform = CGAffineTransform.identity
           }
           _alertCover = cover
        }
 
   }
    

   
   func dismiss() {
       UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut) {
           self._alertCover.alpha = 0
       } completion: { isCompleted in
           if isCompleted {
               self.removeFromSuperview()
               self._alertCover.removeFromSuperview()
               KSLog("销毁了")
           }
           KSLog("外面来了")
       }
   }
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
         self.layer.cornerRadius = 5
        
      }
     required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    
    
}


class AuditOperButton: ThrottleButton {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.cornerRadius = 5
        self.throttleInterval = 1.5
        setTitleColor(UIColor(kshex: "#8991B0"), for: .normal)
        titleLabel?.font = UIFont.systemFont(ofSize: 13)
    }
    override func setTitle(_ title: String?, for state: UIControl.State) {
        super.setTitle(title, for: state)
        if title == "指定改单" {
            self.setImage(UIImage(named: "au_zhiding"), for: .normal)
        } else if title == "移交" {
            self.setImage(UIImage(named: "au_zhuanyi"), for: .normal)
        } else if title == "加签" {
            self.setImage(UIImage(named: "au_jiaqian"), for: .normal)
        } else if title == "知会" {
            self.setImage(UIImage(named: "au_zhihui"), for: .normal)
        } else if title == backTitle {
            self.setImage(UIImage(named: "au_zhiding"), for: .normal)
        } else if title == "查看流程" {
            self.setImage(UIImage(named: "au_chakanliucheng"), for: .normal)
        } else if title == "查看图片" {
            self.setImage(UIImage(named: "au_chakantupian"), for: .normal)
        } else if title == "上传图片" {
            self.setImage(UIImage(named: "au_shangchuan"), for: .normal)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        imageView?.frame = CGRect(x: 5, y: (self.ks_height - 20)/2, width: 20, height: 20)
        titleLabel?.frame = CGRect(x: 35, y: 0, width: self.ks_width - 30, height: self.ks_height)

    }
    
}
